﻿using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Mefisto.Web.MEF;

namespace Mefisto.Web
{
    public class MvcApplication : HttpApplication
    {
        #region Application

        protected void Application_Start()
        {
            // Areas
            AreaRegistration.RegisterAllAreas();
            // Filters
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            // Routes
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            // Bundles
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            BundleTable.EnableOptimizations = true;
            // MEF
            DependencyResolver.SetResolver(new MEFDependencyResolver(new CompositionContainerFactory().CreateDefaultContainer()));
        }

        #endregion

        #region Session

        protected void Session_Start()
        {
            //Gets the application code from the config file
            var applicationCode = Properties.Settings.Default.ApplicationCode;

            //Gets the culture from the cookie
            var cookie = HttpContext.Current.Request.Cookies["Mefisto.CultureCode"];
            var cultureCode = string.Empty;
            cultureCode = cookie != null ? cookie.Value : HttpContext.Current.Request.UserLanguages[0];

            ////On vérifie que la culture est bien disponible pour cette application, sinon on utilise la culture par défaut
            //var resourceProvider = DependencyResolver.Current.GetService<IResourceProvider>();
            //var availableCultures = resourceProvider.GetAvailableCultures(applicationCode);
            ////On vérifie que la culture existe dans les cultures autorisées (fr-FR = fr-FR)
            //if (!availableCultures.ContainsKey(cultureCode))
            //    //Sinon, on vérifie que la culture est une locale d'une culture globale (fr-FR = fr)
            //    if (availableCultures.Any(c => cultureCode.StartsWith(c.Key + "-")))
            //        cultureCode = availableCultures.First(c => cultureCode.StartsWith(c.Key + "-")).Key;
            //    //Sinon, on vérifie que la culture est une globale d'une culture locale (fr = fr-FR)
            //    else if (availableCultures.Any(c => c.Key.StartsWith(cultureCode + "-")))
            //        cultureCode = availableCultures.First(c => c.Key.StartsWith(cultureCode + "-")).Key;
            //    //Sinon, la culture ne correspondant à aucune culture disponible pour cette application, on prend la culture par défaut
            //    else
            //        cultureCode = Properties.Settings.Default.DefaultCulture;

            //Saves the culture code in the cookie
            var localisationCookie = new HttpCookie("Mefisto.CultureCode", cultureCode) { Expires = DateTime.Now.AddYears(1) };
            HttpContext.Current.Response.SetCookie(localisationCookie);

            //Sets session value with these informations
            Session["ApplicationCode"] = applicationCode;
            Session["CultureCode"] = cultureCode;
        }

        #endregion
    }
}